package com.muyi.controller;

import com.muyi.pojo.entity.Comment;
import com.muyi.pojo.entity.Result;
import com.muyi.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "评论模块")
@Slf4j
@RequestMapping("/comment")
@RestController
public class CommentController {
    @Autowired
    private CommentService CommentService;

    /**
     * 查询评论数据
     * @return
     */
    @ApiOperation(value = "查询全部评论")
    @GetMapping
    public Result list(){
        log.info("查询全部评论数据");
        //调用service查询评论数据
        List<Comment> CommentList =  CommentService.list();
        return Result.success(CommentList);
    }


    /**
     * 删除评论
     * @return
     */
    @ApiOperation(value = "删除评论")
    @DeleteMapping("/{comment_id}")
    public Result delete(@PathVariable Integer comment_id){
        log.info("根据comment_id删除评论:{}",comment_id);
        //调用service删除评论
        CommentService.delete(comment_id);
        return Result.success();
    }


    /**
     * 新增评论
     * @return
     */
    @ApiOperation(value = "新增评论")
    @PostMapping
    public Result add(@RequestBody Comment Comment){
        log.info("新增评论: {}" , Comment);
        //调用service新增评论
        CommentService.add(Comment);
        return Result.success();
    }

    @ApiOperation(value = "更新评论")
    @PutMapping
    public Result update(@RequestBody Comment Comment){
        log.info("更新评论信息 : {}", Comment);
        CommentService.update(Comment);
        return Result.success();
    }

    // 根据comment_id查询评论
    @ApiOperation(value = "根据评论id查询评论")
    @GetMapping("/{comment_id}")
    public Result getByCommentId(@PathVariable Integer comment_id){
        log.info("根据comment_id查询评论, comment_id:{}",comment_id);
        Comment Comment = CommentService.getByCommentId(comment_id);
        return Result.success(Comment);
    }
}
